﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace lchealthy
{
    public partial class frmProductShow : Form
    {
        public frmProductShow()
        {
            InitializeComponent();
        }
        DataSet ds;
        private void frmProductShow_Load(object sender, EventArgs e)
        {
            dgvProduct.RowTemplate.Height = 40;
            proBind();
            
        }

        private void proBind()
        {
            if (txtPName.Text == "")
            {
                string sql1 = "select [PID],[PName],[PNum],[PPrice],[PExplain],[PWeight],[PUsing],[PPic] from [ProductInfo]";
                ds = DbHelperOleDb.Query(sql1);
                dgvProduct.AutoGenerateColumns = false;
                dgvProduct.DataSource = ds.Tables[0];
            }
            else
            { 
                btnSearch_Click(null,null); 
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string name = txtPName.Text;
            string no = txtPNo.Text;
            string sql = "select [PID],[PName],[PNum],[PPrice],[PExplain],[PWeight],[PUsing],[PPic] from [ProductInfo] where 1=1";
            if (name!="")
            {
                sql += " and PName like '" + name + "%' ";
            }
          
            
            if (no!="")
            {
                sql += " and PNum = '" + no + "' ";
            }

             ds = DbHelperOleDb.Query(sql);
            dgvProduct.AutoGenerateColumns = false;
            dgvProduct.DataSource = ds.Tables[0];
            }

        private void btnEdite_Click(object sender, EventArgs e)
        {
             int row = -1;
             row = dgvProduct.SelectedCells[0].RowIndex;
             //MessageBox.Show(row.ToString());
             int i = int.Parse(ds.Tables[0].Rows[row]["PID"].ToString());
             //MessageBox.Show(i.ToString());
            //还用判断权限吗？
             frmNewProduct f = new frmNewProduct(i);
             f.Text = "修改产品";
             f.ShowDialog();
             proBind();           
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvProduct.SelectedCells[0].RowIndex;
            int id = int.Parse(ds.Tables[0].Rows[row][0].ToString());//选中的产品的id
            string sql = "delete from [ProductInfo] where  PID=" + id;
            DialogResult n = MessageBox.Show("你确定要删除吗？", "提示！", MessageBoxButtons.YesNo);
            //根据选择的对话框的内容判断是否删除
            if (n == DialogResult.Yes)
            {
                int i = DbHelperOleDb.ExecuteSql(sql);
                if (i == 1)
                {
                    MessageBox.Show("删除成功！");
                    proBind(); 
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void dgvProduct_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgvProduct.Columns[e.ColumnIndex].Name.Equals("产品图片"))
            {
                string path = e.Value.ToString();
                if (File.Exists(Application.StartupPath + path))
                    e.Value = Image.FromFile(Application.StartupPath + path);
                else
                    e.Value = Image.FromFile(Application.StartupPath + "\\pimg\\d.jpg");
            } 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmNewProduct f = new frmNewProduct();
            f.ShowDialog();
            proBind();
        }

        private void txtPName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSearch_Click(null, null);
            }
        }

        private void txtPNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSearch_Click(null, null);
            }
        }
    }
}
